package com.cskaoyan.controller;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.market.*;
import com.cskaoyan.bean.wxcategory.CategoryIndexVO;
import com.cskaoyan.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商场管理模块 商品类目单元
 * 赵维虎
 */
@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;


    @GetMapping("list")
    public BaseRespVo list() {
        CategoryList categoryList = categoryService.list();
        return BaseRespVo.ok(categoryList);
    }


    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryDeleteBo categoryDeleteBo) {
        categoryService.delete(categoryDeleteBo);
        return BaseRespVo.ok();
    }

    //删除时的附属接口
    @GetMapping("l1")
    public BaseRespVo l1() {
        CateGoryLiVo cateGoryLiVo = categoryService.l1();
        return BaseRespVo.ok(cateGoryLiVo);
    }


    //TODO
    @PostMapping("update")
    public BaseRespVo update(@RequestBody CategoryUpdate categoryUpdate) {
        categoryService.update(categoryUpdate);
        return BaseRespVo.ok();
    }


    @PostMapping("create")
    public BaseRespVo create(@RequestBody CategoryCreateBo categoryCreateBo){
        CategoryCreateVo categoryCreateVo = categoryService.create(categoryCreateBo);
        if (categoryCreateVo == null) {
            return BaseRespVo.fail("已有该类目！");
        }else if("a".equals(categoryCreateVo.getDesc())){
            return BaseRespVo.fail("请上传类目照片!");
        }else if ("b".equals(categoryCreateVo.getDesc())){
            return BaseRespVo.fail("请上传类目照片或者父类目！");
        }
        return BaseRespVo.ok(categoryCreateVo);
    }

    @RequestMapping("index")
    public BaseRespVo index(){
        CategoryIndexVO categoryIndexVO = categoryService.index();
        return BaseRespVo.ok(categoryIndexVO);
    }

}
